// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:{
      search_text:'',
      region: ['河北省', '石家庄市', '裕华区'],
      arr:['个人','企业'],
    },
    index:0,
    users:[],
    com:{},
    isShow:false,
  },

  inpIn:function(e){
    var that = this;
    that.setData({
      'data.search_text':e.detail.value
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  btn_search:function(){
    var that = this;
    if(that.data.index==1){
      wx.request({
        url: `http://127.0.0.1:2020/api/company?com_name=${that.data.data.search_text}`,
        method:'GET',
        header:{'content-type':'application/JSON;charset=UTF-8'},
          success:function(res){
            console.log(res.data[0])
            that.setData({
              com:res.data[0],
            })
            var region = that.data.com.com_region;
            region= region.split(',');
            region[0]=region[0].split('"')[1];
            region[1]=region[1].split('"')[1];
            region[2]=region[2].split('"')[1];
            that.setData({
              'com.com_region':region,
              isShow:true
            })
          }
      })
    }
    if(that.data.index==0){
      var data={
        name:that.data.data.search_text,
        region:that.data.data.region,
        id:wx.getStorageSync('user_id'),
      }
      wx.request({
        url: 'http://127.0.0.1:2020/api/search_p',
        method:'GET',
        data:data,
        header:{'content-type':'application/JSON;charset=UTF-8'},
          success:function(res){
            console.log(res.data)
            that.setData({
              users:res.data,
            })
          }
      })

    }
    
  },
  card_show:function(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: `/pages/card_show/card_show?id=${id}`,
    })
  },
  btn_collect:function(e){
    var data={
      post_col_id:wx.getStorageSync('user_id'),
      get_col_id:e.target.id,
    };
    wx.request({
      url: 'http://127.0.0.1:2020/api/collected',
      method:'GET',
      data:data,
      header:{'content-type':'application/JSON;charset=UTF-8'},
        success:function(res){
          console.log(res.data)
        }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showModal({
      title:'温馨提示',
      content:'搜索用户时地区选择是必选项，而企业搜索不需要选择地区',
      confirmText:'确认',
      cancelText:'取消',
      succes:()=>{}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})